<?php


namespace app\index\model;


use think\Model;

class Category extends Model {
    public function articles() {
        return $this->hasMany(Article::class, 'category_id')->
        where('status', 1)->
        hidden(['category_id', 'is_banner', 'status', 'update_time', 'image_url', 'content', 'author']);
    }

    public function child() {
        return $this->hasMany(Category::class, 'pid')->
        hidden(['pid', 'is_banner', 'status', 'update_time', 'create_time', 'is_main']);
    }

    public function getHeaderList() {
        $field = ['id', 'title'];
        $where = ['pid' => 2, 'status' => config('status.mysql.table_normal')];
        $order = ['id' => 'asc'];
        $list =
            $this->
            field($field)->
            where($where)->
            order($order)->
            select();
        return $list;
    }

    public function getArticlesByCategory() {
        $normalStatus = ['status' => config('status.mysql.table_normal')];
        $categoryHidden = ['update_time', 'create_time', 'pid', 'kind', 'is_main', 'listorder'];
        $list = Category::with([
            'articles' => function ($query) {
                $query->
                order('id desc')->
                where(['status' => config('status.mysql.table_normal')])->
                hidden(['content', 'update_time', 'author']);
            }])->
        where($normalStatus)->
        where('kind', '<>', '1')->
        hidden($categoryHidden)->
        select();
        return $list;
    }
}